<?php
class kucun_productControl extends skymvc{
	
	public function __construct(){
		
		parent::__construct();		
	}
	
	function onDefault(){
		M("kucun_product")->update(array(
			"status"=>4
		)," total_num<=0 AND status=2 AND shopid=".SHOPID."");
		 
		$status=get('status','i');
		$status=$status?$status:2; 
		$limit=200;
		$where=" status=$status AND shopid=".SHOPID;
		$url=APPKUCUN."?m=kucun_product&a=my";
		$url.="&status=".$status;
		$start=get('per_page','i');
		$option=array(
			"start"=>$start,
			"limit"=>$limit,
			"order"=>"id DESC",
			"where"=>$where,
		);
		$rscount=true;
		$data=M("kucun_product")->select($option,$rscount);
		if($data){
				foreach($data as $k=>$v){
					$productids[]=$v['productid'];
					 
				}
				$products=M('product')->getListByIds($productids);
			 	 
				foreach($data as $k=>$v){
					$v['title']=$products[$v['productid']]['title'];
					$v['price']=$products[$v['productid']]['price'];
					$v['lower_price']=$products[$v['productid']]['lower_price'];
					$v['imgurl']=$products[$v['productid']]['imgurl'];
					 
					$data[$k]=$v;
				}
			}
	 
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->assign(
			array(
				"data"=>$data,
				"rscount"=>$rscount,
				"pagelist"=>$pagelist,
				 
			)
		);
		$this->smarty->display("kucun_product/index.html");
	}
	
	/*发表产品模型*/
	public function onAdd(){
		 
		 
		$status=get('status','i');
		$status=$status?$status:2; 
		$where=" status=$status ";
		$url=APPKUCUN."?m=kucun_product&a=add";
		//产品总数
		$productids=M("product")->selectCols(array(
			"limit"=>100000,
			"fields"=>"id",
			"where"=>"status=2"
		));
		//库存已有
		$kcproductids=M("kucun_product")->selectCols(array(
			"limit"=>100000,
			"fields"=>"productid",
			"where"=>" shopid=".SHOPID." AND status<=4 "
		));
		if($kcproductids){
			$productids=array_diff($productids,$kcproductids);
		}
		
		if($productids){
			$where.=" AND id in("._implode($productids).") ";
		}else{
			$where.=" AND 1=2 ";
		}
		
		$start=get('per_page','i'); 
		$limit=100;
		$option=array(
			"where"=>$where,
			"order"=>"buy_num DESC",
			"limit"=>$limit,
			"start"=>$start
		);
		$rscount=true;
		
		$kclist=M("product")->select($option,$rscount);
		
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$ymproducts=M("kucun_product")->GetProductIdsBySHop(array("where"=>"shopid=".SHOPID));
		if($kclist){
			foreach($kclist as $k=>$v){
				if(isset($ymproducts[$v['id']])){
					$v['ym_total_num']=$ymproducts[$v['id']]['total_num'];
					$v['ym_buy_num']=$ymproducts[$v['id']]['buy_num'];
					$v['kucun']=1;			
				}
				$kclist[$k]=$v;
			}
		}
		
		$this->smarty->assign(array(
			"kclist"=>$kclist,
			"pagelist"=>$pagelist,
			 
		));
	 
		$this->smarty->display("kucun_product/add.html");
	}
	
	public function onAddKucun(){
		$id=get("id",'i');
		$row=M("kucun_product")->selectRow("id=".$id);
		$this->smarty->assign(
			array(
				"product"=>$row
			)
		);
		echo $data=$this->smarty->fetch("kucun_product/addkucun.html");
	}
	
	public function onaddkucunSave(){
		
		$id=get_post('id','i');
		$total_num=get_post('num','i');
		$row=M("kucun_product")->selectRow(array(
				"where"=>" id=".$id." AND shopid=".SHOPID
		));
		if(empty($row)){
			$this->goALl("数据出错");
		}else{
			M("kucun_product")->update(array(
				"total_num"=>$total_num,
				"status"=>2			
			),"id=".$row['id']);
			$row=M("kucun_product")->selectRow(array(
				"where"=>" id=".$id." AND shopid=".SHOPID
			));
		}
		$this->goAll("数量修改成功",0,$row);
		
	}
	
 
	
	public function onAddProduct(){
			 
			$productid=get_post('productid','i');
			$total_num=get_post('total_num','i');
			$row=M("kucun_product")->selectRow(array(
				"where"=>" productid=".$productid." AND shopid=".SHOPID
			));
			$product=M("product")->selectRow(" id=".$productid);
			if(empty($row)){
				M("kucun_product")->insert(array(
					"shopid"=>SHOPID,
					
					"productid"=>$productid,
					"dateline"=>time(),
					"status"=>2,
					"total_num"=>$total_num,
					"jh_price"=>$product['gys_price'],
				));
			}else{
				M("kucun_product")->update(array(
					"status"=>2,
					"total_num"=>$total_num
				),"id=".$row['id']);
			}
			$this->goall("保存成功");
	}
	
	
	public function onForbid(){
		$id=get('id','i');
		$row=M("kucun_product")->selectRow("id=".$id);
		if($row['shopid']!=SHOPID) $this->goall($this->lang['die_access'],1,0,"/index.php");
		M("kucun_product")->update(array("status"=>4),"id=".$id);
		$this->goall("下线成功");
	}
	
	public function onPass(){
		$id=get('id','i');
		$row=M("kucun_product")->selectRow("id=".$id);
		if($row['shopid']!=SHOPID) $this->goall($this->lang['die_access'],1,0,"/index.php");
		M("kucun_product")->update(array("status"=>2),"id=".$id);
		$this->goall("下线成功");
	}
	
	public function onDelete(){
		$id=get('id','i');
		$row=M("kucun_product")->selectRow("id=".$id);
		if($row['shopid']!=SHOPID) $this->goall($this->lang['die_access'],1,0,"/index.php");
		M("kucun_product")->update(array("status"=>99),"id=".$id);
		$this->goall("删除成功");
	}
	
}

?>